package me.jiatao.taobaotools.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.tencent.smtt.export.external.interfaces.WebResourceRequest;
import com.tencent.smtt.export.external.interfaces.WebResourceResponse;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import java.io.ByteArrayInputStream;

import me.jiatao.taobaotools.Constants;
import me.jiatao.taobaotools.R;
import me.jiatao.taobaotools.TaoBaoApp;
import me.jiatao.taobaotools.utils.InJavaScriptLocalObj;
import me.jiatao.taobaotools.utils.ShowMessageUtils;
import me.jiatao.taobaotools.view.X5WebView;

public class EvaluateActivity extends BaseActivity {

    private X5WebView evaluateWevView;
    private String qrcodeUrl;
    private TextView webViewTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_evaluate);

        qrcodeUrl = getIntent().getStringExtra(Constants.QRCODE_URL_KEY);
        if (TextUtils.isEmpty(TaoBaoApp.htmlString)) {
            ShowMessageUtils.show(activity, "请先   修改评价数量！");
            finish();
            return;
        }

        webViewTitle = (TextView) findViewById(R.id.web_view_title);
        findViewById(R.id.evaluate_toolbar).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        evaluateWevView = (X5WebView) findViewById(R.id.evaluateWevView);
        initWebView();
    }


    private void initWebView() {
        WebSettings webSetting = evaluateWevView.getSettings();
        webSetting.setJavaScriptEnabled(true);

        //扩大比例的缩放
        webSetting.setUseWideViewPort(true);
        webSetting.setSupportZoom(true);
        webSetting.setBuiltInZoomControls(true);
        //自适应屏幕
        webSetting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webSetting.setLoadWithOverviewMode(true);

//        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        webSetting.setAppCacheEnabled(true);
        webSetting.setJavaScriptCanOpenWindowsAutomatically(true);
        webSetting.setDomStorageEnabled(true);

        //webSetting.setUserAgentString("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36");

        evaluateWevView.addJavascriptInterface(new InJavaScriptLocalObj(EvaluateActivity.this, evaluateWevView), "java_obj");

        evaluateWevView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                return true;
            }
        });
        evaluateWevView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(final WebView view, String url) {
                return super.shouldOverrideUrlLoading(view, url);
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                webViewTitle.setText(view.getTitle());

               if (TextUtils.isEmpty(view.getTitle())){
                   ShowMessageUtils.show(activity,"数据加载完成");
               }
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
            }

            @Override
            public WebResourceResponse shouldInterceptRequest(WebView webView, String s) {
                if (s.contains("://rate.taobao.com")) {
                    WebResourceResponse webResourceResponse = new WebResourceResponse("text/html", "utf-8", new ByteArrayInputStream(TaoBaoApp.htmlString.getBytes()));
                    return webResourceResponse;
                }
                return super.shouldInterceptRequest(webView, s);
            }

            @Override
            public WebResourceResponse shouldInterceptRequest(WebView webView, WebResourceRequest webResourceRequest) {
                if (webResourceRequest.getUrl().toString().contains("://rate.taobao.com")) {
                    WebResourceResponse webResourceResponse = new WebResourceResponse("text/html", "utf-8", new ByteArrayInputStream(TaoBaoApp.htmlString.getBytes()));
                    return webResourceResponse;
                }
                return super.shouldInterceptRequest(webView, webResourceRequest);
            }
        });

//        evaluateWevView.loadUrl("http://www.amingyanhao.vip/?type=tqz&uuid=f5d107107f&bsh_bid=2223308635");
        evaluateWevView.loadUrl(qrcodeUrl);
    }


    @Override
    public void onBackPressed() {
        if (evaluateWevView.canGoBack()) {
            evaluateWevView.goBack();
        } else {
            finish();
        }
    }
}
